package com.maybach.core.shiro.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

import java.util.Arrays;
import java.util.HashSet;

/**
 * Created by houenxun on 16/1/12.
 */
public class SimpleRealm extends AuthorizingRealm {
    /**
     * 权限验证
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) this.getAvailablePrincipal(principals);
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        authorizationInfo.setRoles(new HashSet<String>(Arrays.asList("role2")));
        authorizationInfo.setStringPermissions(new HashSet<String>(Arrays.asList("p2", "p3")));
        return authorizationInfo;
    }

    /**
     * 登录验证
     *
     * @param token
     * @return
     * @throws org.apache.shiro.authc.AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        UsernamePasswordToken upToken = (UsernamePasswordToken) token;
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(upToken.getUsername(), String.valueOf(upToken.getPassword()), getName());
        return authenticationInfo;
    }
}